package com.duckygo.towerdefense.game.sprites;

import android.util.Log;
import android.view.MotionEvent;

import com.duckygo.towerdefense.game.scenes.TowerDefenseScene;

public class Tower extends Sprite2D {

	private int cooldown;
	private int range;
	private int cost = 10;
	private int level = 1;
	private int type = 1;
	private double slow = 0;
	
	
	public Tower() {
		
		anchor = CENTER;
		TowerType(1);
		
		//width = 64;
		//height = 64;
		
		//range = 150 * 150;
		
		//texture = TowerDefenseScene.TOWER_TEX;
	}
	
	

	@Override
	public void update(long time) {
		if (cooldown > 0) {
			cooldown --;
			return;
		}
		
		Unit target = null;
		int closest = range;
		
		for (int i = 0; i < parent.children.size(); i++) {
			Sprite sprite = parent.children.get(i);
			if (!(sprite instanceof Unit)) {
				continue;
			}

			
			Unit unit = (Unit)sprite;
			
			int dist = (int) ((unit.x - x) * (unit.x - x) + (unit.y - y) * (unit.y - y));
			if (dist < closest) {
				closest = dist;
				target = unit;
			}
			
			
		}
		
		
		if (target != null) {
			Log.w("Tower", "Found viking " + target);
			
			cooldown = 100;
			parent.addChild(new Projectile(this, target, 10));
		}
		
	}
	
	
	public void levelUp(int level, int range, int cost) {
		
		this.level = level++;
		this.range = range + 150;
		this.cost = cost + 10;
		Log.w("Tower", "Level UP: " + level);
	}
	
	public void TowerType(int type){
		
		Log.w("Tower", "Tower Type: " + type);
		switch (type){
		case 1:
			level = 1;
			width = 64;
			height = 64;
			cost = 10;
			range = 150 * 150;
			
			texture = TowerDefenseScene.TOWER_TEX;
			break;
		case 2:
			level = 1;
			width = 64;
			height = 64;
			cost = 20;
			range = 200 * 200;
			
			texture = TowerDefenseScene.TOWER_TEX; // alta textura
			break;
		case 3:
			level = 1;
			width = 64;
			height = 64;
			cost = 50;
			range = 100 * 100;
			slow = 0.1; // slow down enemy
			
			texture = TowerDefenseScene.TOWER_TEX; // alta textura
		break;
		}

	} 
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}
	
}
